package code.example.zookeeper.base.command.read;

import code.example.zookeeper.ZookeeperHolder;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GetData {
    Logger logger = LoggerFactory.getLogger(GetData.class);
    ZookeeperHolder holder = null;
    public GetData(ZookeeperHolder holder){
        this.holder = holder;
    }

    public String  getDataForWatcher(String path,Watcher watcher) throws  Exception {

        Stat stat = new Stat();
        String  dataResult = getData(path,watcher,stat);
        logger.info("[getData]:register watcher for path:"+path);
        return dataResult;
    }

    public  String getData(String path, Watcher watcher, Stat stat) throws Exception {
        byte [] data =  this.holder.getInstance().getData(path,watcher,stat);
        String dataResult =data ==null?"": new String(data);
        return dataResult;
    }
}
